package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.AttendanceRecordsDTO;
import com.qf.entity.AttendanceRecordsVO;
import com.qf.service.AttendanceRecordsService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName AttendanceRecordsController
 * @Date: 2025/5/23 上午10:46
 * @Description: 描述
 */
@RestController
@RequestMapping("/attendanceRecords")
public class AttendanceRecordsController {

    @Resource
    private AttendanceRecordsService attendanceRecordsService;


    /*
     * 分页查询全部
     * */
    @GetMapping("/findAll")
    public R findAll(AttendanceRecordsDTO attendanceRecordsDTO){
        // 1. 开始分页 或 开启分页
        PageHelper.startPage(attendanceRecordsDTO.getPageNum(),attendanceRecordsDTO.getPageSize());
        // 2. 查询所有的用户
        List<AttendanceRecordsVO> list = attendanceRecordsService.findAll(attendanceRecordsDTO);
        // 3. 将查询出的用户集合交给pageInfo进行分页
        PageInfo<AttendanceRecordsVO> pageInfo = new PageInfo<>(list);
        // 4. 将分页的数据返回给前端
        return R.ok(pageInfo);
    }

    /**
     * 获取考勤统计
     * */
    @GetMapping("/getAttendanceStats")
    public R getAttendanceStats(){
        AttendanceRecordsVO attendanceRecordsVO = attendanceRecordsService.getAttendanceStats();
        return R.ok(attendanceRecordsVO);
    }

    /**
     * 上班打卡
     * @param attendanceRecordsDTO 请求参数
     * @return R
     */
    @PostMapping("/checkIn")
    public R checkIn(@RequestBody AttendanceRecordsDTO attendanceRecordsDTO) {
        Boolean result = attendanceRecordsService.checkIn(attendanceRecordsDTO);

        if (result) {
            return R.ok(20000, "上班打卡成功");
        }
        return R.fail(50000, "上班打卡失败");
    }


    /**
     * 下班打卡
     * @param attendanceRecordsDTO 请求参数
     * @return R
     */
    @PostMapping("/checkOut")
    public R checkOut(@RequestBody AttendanceRecordsDTO attendanceRecordsDTO) {

        Boolean result = attendanceRecordsService.checkOut(attendanceRecordsDTO);
        if (result) {
            return R.ok(20000, "下班打卡成功");
        }
        return R.fail(50000, "下班打卡失败");
    }
}
